package com.team9.fitness.controller;

import com.team9.fitness.service.ZRDDSSendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * 食物信息管理控制器
 */
@RestController
@RequestMapping("/api/food-info")
@CrossOrigin(origins = { "*" }, allowCredentials = "false")
@Slf4j
public class FoodInfoController {

    @Autowired
    private ZRDDSSendService zrddsSendService;

    /**
     * 获取所有食物信息
     */
    @GetMapping
    public CompletableFuture<ResponseEntity<Map<String, Object>>> getAllFoods() {

        log.info("获取所有食物信息请求");

        return zrddsSendService.sendBusinessRequest("FOOD_INFO_GET_ALL", "GET", "/api/food-info",
                null, new HashMap<>())
                .thenApply(response -> {
                    Map<String, Object> result = new HashMap<>();
                    if (response.getSuccess()) {
                        result.put("success", true);
                        result.put("message", "获取食物信息成功");
                        result.put("data", response.getData());
                        log.info("获取食物信息成功");
                    } else {
                        result.put("success", false);
                        result.put("message", response.getMessage() != null ? response.getMessage() : "获取食物信息失败");
                        log.warn("获取食物信息失败: error={}", response.getError());
                    }
                    return ResponseEntity.ok(result);
                })
                .exceptionally(throwable -> {
                    log.error("获取食物信息异常", throwable);
                    Map<String, Object> result = new HashMap<>();
                    result.put("success", false);
                    result.put("message", "获取食物信息服务异常，请稍后重试");
                    return ResponseEntity.ok(result);
                });
    }

    /**
     * 根据ID获取食物信息
     */
    @GetMapping("/{id}")
    public CompletableFuture<ResponseEntity<Map<String, Object>>> getFoodById(@PathVariable Long id) {

        log.info("根据ID获取食物信息请求: id={}", id);

        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("id", id);

        return zrddsSendService.sendBusinessRequest("FOOD_INFO_GET_BY_ID", "GET", "/api/food-info/" + id,
                null, requestBody)
                .thenApply(response -> {
                    Map<String, Object> result = new HashMap<>();
                    if (response.getSuccess()) {
                        result.put("success", true);
                        result.put("message", "获取食物信息成功");
                        result.put("data", response.getData());
                        log.info("获取食物信息成功: id={}", id);
                    } else {
                        result.put("success", false);
                        result.put("message", response.getMessage() != null ? response.getMessage() : "食物信息不存在");
                        log.warn("获取食物信息失败: id={}, error={}", id, response.getError());
                    }
                    return ResponseEntity.ok(result);
                })
                .exceptionally(throwable -> {
                    log.error("获取食物信息异常: id={}", id, throwable);
                    Map<String, Object> result = new HashMap<>();
                    result.put("success", false);
                    result.put("message", "获取食物信息服务异常，请稍后重试");
                    return ResponseEntity.ok(result);
                });
    }

    /**
     * 根据分类获取食物信息
     */
    @GetMapping("/category/{category}")
    public CompletableFuture<ResponseEntity<Map<String, Object>>> getFoodsByCategory(@PathVariable String category) {

        log.info("根据分类获取食物信息请求: category={}", category);

        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("category", category);

        return zrddsSendService.sendBusinessRequest("FOOD_INFO_GET_BY_CATEGORY", "GET",
                "/api/food-info/category/" + category, null, requestBody)
                .thenApply(response -> {
                    Map<String, Object> result = new HashMap<>();
                    if (response.getSuccess()) {
                        result.put("success", true);
                        result.put("message", "获取分类食物信息成功");
                        result.put("data", response.getData());
                        log.info("获取分类食物信息成功: category={}", category);
                    } else {
                        result.put("success", false);
                        result.put("message", response.getMessage() != null ? response.getMessage() : "获取分类食物信息失败");
                        log.warn("获取分类食物信息失败: category={}, error={}", category, response.getError());
                    }
                    return ResponseEntity.ok(result);
                })
                .exceptionally(throwable -> {
                    log.error("获取分类食物信息异常: category={}", category, throwable);
                    Map<String, Object> result = new HashMap<>();
                    result.put("success", false);
                    result.put("message", "获取分类食物信息服务异常，请稍后重试");
                    return ResponseEntity.ok(result);
                });
    }

    /**
     * 搜索食物信息
     */
    @GetMapping("/search")
    public CompletableFuture<ResponseEntity<Map<String, Object>>> searchFoods(@RequestParam String keyword) {

        log.info("搜索食物信息请求: keyword={}", keyword);

        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("keyword", keyword);

        return zrddsSendService.sendBusinessRequest("FOOD_INFO_SEARCH", "GET", "/api/food-info/search",
                null, requestBody)
                .thenApply(response -> {
                    Map<String, Object> result = new HashMap<>();
                    if (response.getSuccess()) {
                        result.put("success", true);
                        result.put("message", "搜索食物信息成功");
                        result.put("data", response.getData());
                        log.info("搜索食物信息成功: keyword={}", keyword);
                    } else {
                        result.put("success", false);
                        result.put("message", response.getMessage() != null ? response.getMessage() : "搜索食物信息失败");
                        log.warn("搜索食物信息失败: keyword={}, error={}", keyword, response.getError());
                    }
                    return ResponseEntity.ok(result);
                })
                .exceptionally(throwable -> {
                    log.error("搜索食物信息异常: keyword={}", keyword, throwable);
                    Map<String, Object> result = new HashMap<>();
                    result.put("success", false);
                    result.put("message", "搜索食物信息服务异常，请稍后重试");
                    return ResponseEntity.ok(result);
                });
    }

    /**
     * 获取所有食物分类
     */
    @GetMapping("/categories")
    public CompletableFuture<ResponseEntity<Map<String, Object>>> getAllCategories() {

        log.info("获取所有食物分类请求");

        return zrddsSendService.sendBusinessRequest("FOOD_INFO_GET_CATEGORIES", "GET", "/api/food-info/categories",
                null, new HashMap<>())
                .thenApply(response -> {
                    Map<String, Object> result = new HashMap<>();
                    if (response.getSuccess()) {
                        result.put("success", true);
                        result.put("message", "获取食物分类成功");
                        result.put("data", response.getData());
                        log.info("获取食物分类成功");
                    } else {
                        result.put("success", false);
                        result.put("message", response.getMessage() != null ? response.getMessage() : "获取食物分类失败");
                        log.warn("获取食物分类失败: error={}", response.getError());
                    }
                    return ResponseEntity.ok(result);
                })
                .exceptionally(throwable -> {
                    log.error("获取食物分类异常", throwable);
                    Map<String, Object> result = new HashMap<>();
                    result.put("success", false);
                    result.put("message", "获取食物分类服务异常，请稍后重试");
                    return ResponseEntity.ok(result);
                });
    }
}
